//
//  ZYEmotionPageView.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/7/4.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit

//定义cell的可重用标识
private let CZEmoticonPageViewCellId = "CZEmoticonPageViewCellId"

class ZYEmotionPageView: UICollectionView {
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        //
        
        setupUI()
        
    }

    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置UI
    func setupUI() {
        
        //7.7====
        //backgroundColor = UIColor.whiteColor()
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
            
        //注册cell
        registerClass(ZYEmotionPageViewCell.self, forCellWithReuseIdentifier: CZEmoticonPageViewCellId)
        
        //设置代理
        dataSource = self
        
        //设置layOut
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置itemSize
        layout.itemSize = CGSize(width: SCREENW, height: 216 -  37)
        
        //设置间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        //设置滚动的方向
        layout.scrollDirection = .Horizontal
        
        
        
    }
}


extension ZYEmotionPageView : UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        //使用模型里面的
        return ZYEmotionTools.sharedTools.allEmotion.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        print(ZYEmotionTools.sharedTools.allEmotion[section].count)
        //返回页数
        return ZYEmotionTools.sharedTools.allEmotion[section].count
        
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZEmoticonPageViewCellId, forIndexPath: indexPath) as! ZYEmotionPageViewCell
        
        cell.backgroundColor = RandomColor()
        cell.indexPath = indexPath
        
        //给cell进行赋值
        cell.emotions = ZYEmotionTools.sharedTools.allEmotion[indexPath.section][indexPath.item]
        
        cell.backgroundColor = self.backgroundColor
        return cell
        
    }

}



































